<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Cache;
use think\cache\driver\Redis;
use think\facade\Session;
use think\facade\Db;

class AdminZavote extends Model
{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
               //按标题查找
               if ($title = input("title")) {
                   $where[] = ["title", "like", "%" . $title . "%"];
               }
               //按标题查找
               if ($datetime = null) {
                    // $where[] = ["datetime", "like", "%" . 0 . "%"];
                }else{
                    $where[] = ["datetime", "like", "%" . 0 . "%"];
                }
                $uid = Session::get('admin.id');
                //用户权限
               if ($uid == 1) {
                    
                }else{
                    $where[] = ["yhid", "=",  $uid ];
                }
        $list = self::order('id','desc')->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    // 前台获取投票信息
    public static function gettpbyid($id,$yonghuid)
    {
        // $arrid = Cache::get('tpgettpbyid:'.$id);
        $arrid = Cache::store('redis')->get('tpgettpbyid:'.$id);
        // dump($arrid);
        // dump(empty($arrid));
        if(empty($arrid)){
            $res = self::find($id);

            $zhenres = $res;

            $resgzh = Db::name('admin_zezhengzhpz')->where('appid',$zhenres['zdlgzh'])->find();
            $zhenres['yhsfsycs1111'] = 9981111;
            $zhenres['gzhid'] = $resgzh['cid'];
            $zhenres['apphao'] = $resgzh['apphao'];

            $nowtime = date("Y-m-d H:m:s");
            if($nowtime < $zhenres['begintime']){
                $zhenres['wjy'] = 0;
            }elseif($nowtime > $zhenres['stoptime']){
                $zhenres['wjy'] = 2;
            }else{
                $zhenres['wjy'] = 1;
            }
            $timeend = strtotime($zhenres['stoptime']) - strtotime($nowtime);
            $zhenres['timeend'] = $timeend;
            
            $zhenres['timeendd'] = floor($timeend / (60 * 60 * 24));
            $zhenres['timeendh'] = $timeend / (60 * 60) % 24;
            $zhenres['timeendm'] = $timeend / 60 % 60;
            $zhenres['timeends'] = $timeend % 60;
            
            if($zhenres['lunbotuxs'] == 1){
                $tuarray = array_filter(array($zhenres['fmimage'],$zhenres['lunboimgyi'],$zhenres['lunboimger'],$zhenres['lunboimgsan']));
            }else{
                $tuarray = array($zhenres['fmimage']);
            }
            $zhenres['tuarray'] = $tuarray;
            $zhenres['dqurl'] =  THTPADMIN;
            if($zhenres['fenzuname']){
                $res22 = explode('/',$zhenres['fenzuname']);
                $zhenres['fenzunamearr'] = array_filter($res22);
                $res33 = array();
                for($i=0;$i < count($zhenres['fenzunamearr']);$i++){
                    $res33[$i] = "{cate_name:'".$zhenres['fenzunamearr'][$i]."',}";
                }
                $zhenres['fenzunamearr1'] = $res33;
            }else{
                $zhenres['fenzunamearr1'] = '';
            }
            
            // Cache::set('tpgettpbyid:'.$id,$zhenres,310);
            Cache::store('redis')->set('tpgettpbyid:'.$id,$zhenres,310);
        } else {
            $zhenres = $arrid;
        }
        
        if($yonghuid){
            $nowt = date("Y-m-d H:m:s");
            if($zhenres['baoming']==1){
                if($nowt > $zhenres['bmjstime']){
                    $zhenres['sfbmtime'] = 0;
                    $zhenres['yhsfybm'] = 0;
                    $zhenres['yhsfybmnum'] = 0;
                    $zhenres['yhsfjxbm'] = 0;
                }elseif($nowt < $zhenres['bmkstime']){
                    $zhenres['sfbmtime'] = 0;
                    $zhenres['yhsfybm'] = 0;
                    $zhenres['yhsfybmnum'] = 0;
                    $zhenres['yhsfjxbm'] = 0;
                }else{
                    $zhenres['sfbmtime'] = 1;
                    $whe['uid']=$yonghuid;
                    $whe['biaoid']=$zhenres['id'];
                    $sfbmres = Db::name("admin_zccandidate")->where($whe)->count();
                    if($sfbmres == 0){
                        $zhenres['yhsfybm'] = 0;
                        $zhenres['yhsfybmnum'] = 0;
                        $zhenres['yhsfjxbm'] = 0;
                    }else{
                        $zhenres['yhsfybm'] = 1;
                        $zhenres['yhsfybmnum'] = $sfbmres;
                        
                        if($sfbmres == $zhenres['bmnum']){
                            $zhenres['yhsfjxbm'] = 1;
                        }else{
                            $zhenres['yhsfjxbm'] = 0;
                        }

                    }
                }
            }else{
                $zhenres['sfbmtime'] = 0;
                $zhenres['yhsfybm'] = 0;
                $zhenres['yhsfybmnum'] = 0;
                $zhenres['yhsfjxbm'] = 0;
            }
            
            if($zhenres['cishu']==0){
                // $timetoday = strtotime(date("Y-m-d",time()));   //今日00时间戳
                // $time2today = strtotime(date('Y-m-d',strtotime('+1 day')));  //  今日结束时间 23:59戳
    
                // $wz['biaoid']=$id;
                // $wz['openid']=$yonghuid;
                // $resz = Db::name('admin_zdvoter')->where($wz)->where([
                //     ['datetime','>',$timetoday],
                //     ['datetime','<',$time2today]
                // ])->sum('cishu');

                $yonghutousum = Cache::get('tpyonghutousum:'.$id.$yonghuid);
                if($yonghutousum==""){
                    $resz = 0;
                } else {
                    $resz = $yonghutousum;
                }

                if($resz >= $zhenres['daycishu']){
                    $zhenres['yhsfytw'] = 1;
                    $zhenres['yhsfsycs'] = 0;
                }else{
                    $zhenres['yhsfytw'] = 0;
                    $zhenres['yhsfsycs'] = $zhenres['daycishu'] - $resz;
                }
            }else{
                // $wz['biaoid']=$id;
                // $wz['openid']=$yonghuid;
                // $resz = Db::name('admin_zdvoter')->where($wz)->sum('cishu');

                $yonghutousum = Cache::get('tpyonghutousum:'.$id.$yonghuid);
                if($yonghutousum==""){
                    $resz = 0;
                } else {
                    $resz = $yonghutousum;
                }

                if($resz >= $zhenres['daycishu']){
                    $zhenres['yhsfytw'] = 1;
                    $zhenres['yhsfsycs'] = 0;
                }else{
                    $zhenres['yhsfytw'] = 0;
                    $zhenres['yhsfsycs'] = $zhenres['daycishu'] - $resz;
                }
            }
        }else{
            $nowt = date("Y-m-d H:m:s");
            if($nowt > $zhenres['bmjstime']){
                $zhenres['sfbmtime'] = 0;
            }elseif($nowt < $zhenres['bmkstime']){
                $zhenres['sfbmtime'] = 0;
            }else{
                $zhenres['sfbmtime'] = 1;
            }
            $zhenres['yhsfybm'] = 0;
            $zhenres['yhsfybmnum'] = 0;
            $zhenres['yhsfjxbm'] = 0;
            $zhenres['yhsfytw'] = 0;
            $zhenres['yhsfsycs'] = 998;
        }
        return ['code'=>200,'message'=>'成功','data'=>$zhenres];
    }

    // 增加点击量
    public static function tpcat($pid)
    {
        $res = self::find($pid);
        $rand=rand(1,1);
        $data['cat']=$res['cat']+$rand;
        self::where('id', $pid)->update($data);
        return ['code'=>200,'message'=>'成功'];
    }


}
